const mongodb = require('../middleware/mongo');
const config = require('../../config/config');
const ObjectID = require('mongodb').ObjectID;
class MongodbModules {
    //获取数量
    static async count(collectionName, json) {
        let data = [];
        try {
            data = await mongodb.aggregate(config.mongodb.prefix + collectionName, json);
        } catch (error) {
            data = []
        }
        if (!data[0]) {
            data.push({ count: 0 })
        }
        return data;
    }
    //查询
    static async find(collectionName, json = {}, sort = {}, skip = 0, limit = 0) {
        let datas = [];
        try {
            datas = await mongodb.find(config.mongodb.prefix + collectionName, json, sort, skip, limit);
        } catch (err) {
            datas = [];
        }
        return datas;
    }
    //插入
    static async insert(collectionName, json) {
        let data = {};
        try {
            data = await mongodb.insert(config.mongodb.prefix + collectionName, json);
        } catch (err) {
            data.insertedCount = 0
        }
        return data;
    }
    //批量插入
    static async insertMany(collectionName, json) {
        let data = {};
        try {
            data = await mongodb.insertMany(config.mongodb.prefix + collectionName, json);
        } catch (err) {
            data.insertedCount = 0
        }
        return data;
    }
    //修改
    static async update(collectionName, json1, json2, json3 = {}) {
        let datas = {};
        try {
            datas = await mongodb.update(config.mongodb.prefix + collectionName, json1, json2, json3);
        } catch (error) {
            datas.result = { n: 0 };
        }
        return datas['result'];
    }
    //联合查询
    static async join(collectionName, json) {
        let data = [];
        try {
            data = await mongodb.aggregate(config.mongodb.prefix + collectionName, json);
        } catch (error) {
            data = []
        }
        return data;
    }
    //求和
    static async sum(collectionName, json) {
        let data = [];
        try {
            data = await mongodb.aggregate(config.mongodb.prefix + collectionName, json);
        } catch (error) {
            data = []
        }
        if (!data[0]) {
            data.push({ sum: 0 })
        }
        return data;
    }
    static async updates(collectionName, json1, json2) {
        let datas = {};
        try {
            datas = await mongodb.updates(config.mongodb.prefix + collectionName, json1, json2);
        } catch (error) {
            datas.result = { n: 0 };
        }
        return datas['result'];
    }
    //删除全部
    static async deleteMany(collectionName, json){
        let datas = {};
        try {
            datas = await mongodb.deleteMany(config.mongodb.prefix + collectionName, json);
        } catch (error) {
            datas.result = { n: 0 };
        }
        return datas['result'];
    }
    static async getObjectID(key) {
        return ObjectID(key);
    }
}
module.exports = MongodbModules;